package com.llp.javaweb.servlet;

import com.google.gson.Gson;
import com.llp.javaweb.pojo.Product;
import com.llp.javaweb.service.ProductService;
import com.llp.javaweb.service.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @BelongsProject: ascent-web-master
 * @BelongsPackage: com.llp.javaweb.servlet
 * @Author: bovan
 * @Date: 2022/11/6 22:17
 * @Description:
 */
@WebServlet("/product/selectAllProducts")
public class SelectAllProductServlet extends HttpServlet {
    ProductService service = new ProductServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/templates/product/products_show.html").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Product> product = service.selectAllProducts();
        Gson gson=new Gson();
        //存在则返回userexist
        if (product != null) {
            resp.getWriter().write(gson.toJson(product));
        }else{
            resp.getWriter().write("fail");
        }
    }
}
